<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class AdminLogModel
{
    public static function add($param){
        return DB::table('t_admin_log')->insert($param);
    }

    public static function queryCount($where=[],$param=[]){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin_log as al')
                ->leftJoin('t_admin as a','al.admin_id','=','a.id')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('al.id');
        }
        return DB::table('t_admin_log as al')
            ->leftJoin('t_admin as a','al.admin_id','=','a.id')
            ->where($where)
            ->count('al.id');
    }

    public static function queryAll($where=[],$param=[],$page){
        $field = ['al.id','a.username','al.ip','al.remark','al.details','al.create_time'];
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin_log as al')
                ->leftJoin('t_admin as a','al.admin_id','=','a.id')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field);
        }
        return DB::table('t_admin_log as al')
            ->leftJoin('t_admin as a','al.admin_id','=','a.id')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field);
    }

    public static function queryRegisterCount($where,$param){
        if($param){
            return DB::table('t_admin_log')
                ->where($where)
                ->where('create_time','>=',$param['begTime'])
                ->where('create_time','<=',$param['endTime'])
                ->count('id');
        }
        return DB::table('t_admin_log')
            ->where($where)
            ->count('id');
    }

    public static function queryLoginCount($where,$param){
        if($param){
            return DB::table('t_admin_log')
                ->where($where)
                ->where('create_time','>=',$param['begTime'])
                ->where('create_time','<=',$param['endTime'])
                ->distinct('admin_id')
                ->count('id');
        }
        return DB::table('t_admin_log')
            ->where($where)
            ->distinct('admin_id')
            ->count('id');
    }
}